﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using Common;

public partial class website_admin_members_memberinfoeditor : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin();

        switch (RequestBase.GetFormString("otype"))
        {
            case "getcitylist":
                LoadCityDataList();
                break; 
        }

        /// 保存设置
        if (RequestBase.GetFormString("___EVENTTARGET") == "save")
        {
            Save();
        }

        if (!IsPostBack)
        {
            DropdownListDataBind();
            if (RequestBase.GetQueryString("otype") == "modify")
                Initialize();
        }
    }


    #region 下拉菜单数据邦定
    void DropdownListDataBind()
    {
        DataTable dt = BLL.CommonBLL.Select(
            "RegionBase",
            "RegionName, RegionId",
            " AND DeletionStateCode = 0 AND Layer = 1 AND ParentId = " + txtCountryId.Value.Trim() + " ORDER BY RegionName ASC");

        selProvinceList.Items.Add(new ListItem("-- 请选择省份 --", ""));
        foreach (DataRow dr in dt.Select(""))
        {
            selProvinceList.Items.Add(new ListItem(dr["RegionName"].ToString().Trim(), dr["RegionId"].ToString().Trim()));
        }
        dt.Dispose();

        selProvinceList.Attributes.Add("onchange", "LoadRegionList('event', 'city', 1);LoadRegionList('event', 'countie', 2);");
    }
    /// <summary>
    /// 获取子类明细
    /// </summary>
    void LoadCityDataList()
    {
        string parentid = RequestBase.GetFormString("parentid");
        StringBuilder selOptions = new StringBuilder();
        string items = "<option value=\"{0}\">{1}</option>";

        string layer = "0";
        switch (RequestBase.GetFormString("_layername"))
        {
            case "city":
                layer = "2";
                selOptions.Append(string.Format(items, "0", "-- 请选择城市 --"));
                break;
            case "countie":
                layer = "3";
                selOptions.Append(string.Format(items, "0", "-- 请选择区县 --"));
                break;
        } 

        DataTable dat = BLL.CommonBLL.Select(
            "RegionBase",
            "RegionName, RegionId",
            " AND DeletionStateCode = 0 AND ParentId = " + parentid + " AND Layer=" + layer + " ORDER BY RegionName ASC");

        foreach (DataRow dr in dat.Select(""))
        {
            selOptions.Append(string.Format(items, dr["RegionId"].ToString(), dr["RegionName"].ToString()));
        }

        dat.Clear();
        dat.Dispose();
        BLL.AjaxHelper.ResponseWrite(selOptions.ToString());
    }
    #endregion 

    #region 数据初始化
    void Initialize()
    {
        Model.MembershipBase _obj = BLL.MembershipBase.GetObjById(RequestBase.GetQueryString("userid"));
        UserObj = _obj; 
    }

    Model.MembershipBase UserObj
    {
        get
        {
            Model.MembershipBase _userObj = new Model.MembershipBase();

            if (!string.IsNullOrEmpty(RequestBase.GetQueryString("userid")))
                _userObj.UserId = RequestBase.GetQueryString("userid");
 
            _userObj.UserName = this.txtUsername.Value; 
            _userObj.FullName = this.txtFullName.Value;
            _userObj.MobileAlias = this.txtMobileAlias.Value;
            _userObj.Birthday = DateTime.Parse(string.IsNullOrEmpty(this.txtBirthday.Value.Trim()) ? "1754-01-01": this.txtBirthday.Value.Trim());
            _userObj.Gender = int.Parse(this.rbtGenderList.SelectedValue);
            _userObj.RegionId = int.Parse(string.IsNullOrEmpty(this.txtRegionId.Value) ? "0" : this.txtRegionId.Value);
            _userObj.CountryId = int.Parse(string.IsNullOrEmpty(this.txtCountryId.Value) ? "0" : this.txtCountryId.Value);
            _userObj.ProvinceId = int.Parse(string.IsNullOrEmpty(this.txtProvinceId.Value) ? "0" : this.txtProvinceId.Value);
            _userObj.CityId = int.Parse(string.IsNullOrEmpty(this.txtCityId.Value) ? "0" : this.txtCityId.Value);
            _userObj.CountieId = int.Parse(string.IsNullOrEmpty(this.txtCountieId.Value) ? "0" : this.txtCountieId.Value);
            _userObj.RegionInfo = this.txtRegionInfo.Value; 
            _userObj.Password = this.txtPassword.Value;
            _userObj.Address = this.txtAddress.Value;
            _userObj.Tel = this.txtTel.Value;
            _userObj.ZIP = this.txtZIP.Value;
            _userObj.Mobile = this.txtMobile.Value;
            _userObj.Email = this.txtEmail.Value; 
            _userObj.PasswordQuestion = "";
            _userObj.PasswordAnswer = "";
            _userObj.IsApproved = (this.selIsApproved.Value == "1") ? true : false;
            _userObj.IsLockedOut = (this.selIsLockedOut.Value == "1") ? true : false;
            _userObj.CreateDate = DateTime.Now;
            _userObj.Comment = "";

            return _userObj;
        }
        set
        {
            if (value != null)
            {
                this.txtUsername.Value = value.UserName;
                this.txtFullName.Value = value.FullName;
                this.txtMobileAlias.Value = value.MobileAlias;
                this.txtBirthday.Value = value.Birthday.ToString("yyyy-MM-dd").Replace("1754-01-01", "");
                this.rbtGenderList.SelectedValue = value.Gender.ToString();
                this.txtRegionId.Value = value.RegionId.ToString();
                this.txtProvinceId.Value = value.ProvinceId.ToString();
                this.txtCityId.Value = value.CityId.ToString();
                this.txtCountieId.Value = value.CountieId.ToString();
                this.txtRegionInfo.Value = value.RegionInfo;
                selProvinceList.Value = value.ProvinceId.ToString();
                this.txtPassword.Value = value.Password;
                this.txtAddress.Value = value.Address;
                this.txtTel.Value = value.Tel;
                this.txtZIP.Value = value.ZIP;
                this.txtMobile.Value = value.Mobile;
                this.txtEmail.Value = value.Email;
                this.selIsApproved.Value = value.IsApproved ? "1" : "0";
                this.selIsLockedOut.Value = value.IsLockedOut ? "1" : "0";
                this.txtCreatedDate.Value = value.CreateDate.ToString("yyyy-MM-dd").Replace("1754-01-01", "");
                this.txtLastLoginDate.Value = value.LastLoginDate.ToString("yyyy-MM-dd").Replace("1754-01-01", "");
            }
        }
    } 
    #endregion

    #region 保存数据
    void Save()
    {
        Model.Enum.MembershipCreateStatus status = Model.Enum.MembershipCreateStatus.ProviderError;

        if (RequestBase.GetQueryString("otype").Trim() == "add")
        {
            Model.MembershipBase _obj = (new BLL.MembershipBase()).CreateUser(UserObj, out status);
        }

        if (RequestBase.GetQueryString("otype").Trim() == "modify")
        {
            Model.MembershipBase _obj = (new BLL.MembershipBase()).UpdateUser(UserObj, out status);
        }

        switch ((int)status)
        {
            case 0:
                JScript.AlertAndReturnValueToParent("保存成功!", string.Format("window.location.href='membersmaglist.aspx?groupid={0}';", RequestBase.GetQueryString("groupid")));
                break;
            default:
                JScript.Alert(status.ToString());
                break;
        }
    }
    #endregion
}
